﻿using System.Data.Entity;
using Icms.Core.Model;

namespace Icms.Data
{
    public class Db : DbContext
    {
        public DbSet<ReportTemplateGroup> ReportTemplateGroups { get; set; }
        public DbSet<User> Users { get; set; }
        public DbSet<Role> Roles { get; set; }
        public DbSet<Parameter> Parameters { get; set; }
        public DbSet<Classifier> Classifiers { get; set; }
        public DbSet<Category> Categories { get; set; }
        public DbSet<CaseType> CaseTypes { get; set; }
        public DbSet<Court> Courts { get; set; }
        public DbSet<CourtType> CourtTypes { get; set; }
        public DbSet<Template> Templates { get; set; }
        public DbSet<CourtCfg> CourtCfgs { get; set; }
        public DbSet<Case> Cases { get; set; }
        public DbSet<Party> Parties { get; set; }
        public DbSet<Person> Persons { get; set; }
        public DbSet<File> Files { get; set; }
        public DbSet<Hearing> Hearings { get; set; }
        public DbSet<Alert> Alerts { get; set; }
        public DbSet<AlertUser> AlertUsers { get; set; }
        public DbSet<Status> Statuses { get; set; }
        public DbSet<Team> Teams { get; set; }
        public DbSet<Ruling> Rulings { get; set; }
        public DbSet<ReportRequest> ReportRequests { get; set; }
        public DbSet<Message> Messages { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<User>().HasMany(o => o.Roles).WithMany(i => i.Users)
            .Map(m =>
            {
                m.MapLeftKey("UserId");
                m.MapRightKey("RoleId");
            });

            modelBuilder.Entity<Hearing>().HasMany(o => o.Parties).WithMany(o => o.Hearings)
                .Map(o =>
                {
                    o.MapLeftKey("HearingId");
                    o.MapRightKey("PartyId");
                });

            modelBuilder.Entity<Case>().HasMany(o => o.CaseTypes).WithMany(m => m.Cases)
                .Map(o =>
                {
                    o.MapLeftKey("CaseId");
                    o.MapRightKey("CaseTypeId");
                });

            modelBuilder.Entity<Person>().ToTable("Persons");
            modelBuilder.Entity<Status>().ToTable("Statuses");

            base.OnModelCreating(modelBuilder);
        }
    }
}
